package com.rp.common.config.shiro;

import com.rp.common.utils.NullIs;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

/**
 * 类说明:
 *
 * @author ****
 * @date 2021-03-19 11:07  新建
 */

public class SessionConfig extends DefaultWebSessionManager {

    public SessionConfig(){
        super();
    }

    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        String value = WebUtils.toHttp(request).getHeader("Authorization");
        if(!NullIs.isEmpty(value)){
            String type = WebUtils.toHttp(request).getHeader("Authorization").split(" ")[0];
            String id = WebUtils.toHttp(request).getHeader("Authorization").split(" ")[1];
            if (!NullIs.isEmpty(type) && "rupeng".equals(type) && !NullIs.isEmpty(id)) {
                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, "Stateless request");
                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, id);
                request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
                return id;
            }
        }
        return super.getSessionId(request, response);

    }
}


